home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Environments / AppMkr151#2 / Libraries / MPW / AMLibraryC / Dragging.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-27  |  6.7 KB  |  316 lines  |  [TEXT/MPS ]

  1. /* © 1991, Bowers Development Corp. */
  2. /* Dragging.c */
  3.  
  4. #include <QuickDraw.h>
  5. #include <ToolUtils.h>
  6. #include <TextEdit.h>
  7. #include <Windows.h>
  8.  
  9. #include "Globals.h"
  10. #include "ResourceDefs.h"    
  11.  
  12. #include "Dragging.h"
  13.  
  14. #pragma segment Dragging
  15.  
  16. /*----------*/
  17. static short Abs (short x);
  18. static short Abs (short x)
  19. {
  20.     return ((x < 0) ? (-x) : (x));
  21. } /*Abs*/
  22.  
  23. /*----------*/
  24. Boolean StartMove  (Point        *startPt, 
  25.                     short        *constraint)
  26. {
  27.     #define    slop    2
  28.  
  29.     Rect            slopRect;
  30.     Point            mousePt;
  31.  
  32.     #define sP        (*startPt)
  33.     SetRect (&slopRect, sP.h, sP.v, sP.h + 1, sP.v + 1);
  34.     #undef sP
  35.  
  36.     InsetRect (&slopRect, -slop, -slop);
  37.  
  38.     mousePt = *startPt;
  39.     while (StillDown () && PtInRect (mousePt, &slopRect)) {
  40.         GetMouse (&mousePt);
  41.     }
  42.  
  43.     if (StillDown ()) {
  44.         *constraint = noConstraint;
  45.         if ((curEvent.modifiers & optionKey) != 0) {
  46.             if (Abs (mousePt.h - (*startPt).h)
  47.             >  Abs (mousePt.v - (*startPt).v)) {
  48.                 *constraint = hAxisOnly;
  49.             } else {
  50.                 *constraint = vAxisOnly;
  51.             }
  52.         }
  53.     }
  54.     
  55.     *startPt = mousePt;
  56.     if ((*startPt).h > (slopRect).right)  { (*startPt).h = (slopRect).right;    }
  57.     if ((*startPt).h < (slopRect).left)   { (*startPt).h = (slopRect).left - 1;    }
  58.     if ((*startPt).v > (slopRect).bottom) { (*startPt).v = (slopRect).bottom;    }
  59.     if ((*startPt).v < (slopRect).top)    { (*startPt).v = (slopRect).top - 1;    }
  60.     
  61.     return (StillDown ());
  62. } /*StartMove*/
  63.  
  64. /*----------*/
  65. Boolean TrackMove  (RgnHandle            dragRgn,
  66.                     Point                startPt,
  67.                     Rect                bounds,
  68.                     short                constraint,
  69.                     short                *deltaH,
  70.                     short                *deltaV,
  71.                     DragGrayRgnProcPtr    actionProc)
  72. {
  73.     #undef slop
  74.     #define slop    20
  75.     
  76.     Rect            limitRect;
  77.     Rect            slopRect;
  78.     long            delta;
  79.     register Rect        *dragBBox;
  80.     
  81.     dragBBox = &(**dragRgn).rgnBBox;
  82.     SetRect (&limitRect,
  83.                 bounds.left   - dragBBox->left,
  84.                 bounds.top    - dragBBox->top,
  85.                 bounds.right  - dragBBox->right + 1,
  86.                 bounds.bottom - dragBBox->bottom + 1);
  87.     
  88.     OffsetRect (&limitRect, startPt.h, startPt.v);
  89.     slopRect = limitRect;
  90.     InsetRect (&slopRect, -slop, -slop);
  91.     
  92.     delta = DragGrayRgn (dragRgn, startPt, &limitRect,
  93.                             &slopRect, constraint, actionProc);
  94.     *deltaV = HiWord (delta);
  95.     *deltaH = LoWord (delta);
  96.  
  97.     if (( delta  == 0)
  98.     ||  (*deltaH == 0x8000)
  99.     ||  (*deltaV == 0x8000)) {
  100.         return (false);
  101.     } else {
  102.         return (true);
  103.     }
  104. } /*TrackMove*/
  105.  
  106. /*----------*/
  107. Boolean TrackRange (Point        anchorPt,
  108.                     Rect        *range)
  109. {
  110.     PenState        savePen;
  111.     Point            curPos;
  112.     Point            newPos;
  113.     long            lastTicks;
  114.  
  115.     GetPenState (&savePen);
  116.     PenNormal ();
  117.     PenMode (notPatXor);
  118.     PenPat (&qd.gray);
  119.  
  120.     newPos = anchorPt;
  121.     curPos = newPos;
  122.     Pt2Rect (anchorPt, curPos, range);
  123.     FrameRect (range);    /*Draw*/
  124.  
  125.     lastTicks = 0;    
  126.     while (StillDown ()) {
  127.         if (TickCount () >= lastTicks + 2) {
  128.             GetMouse (&newPos);
  129.             if (!EqualPt (newPos, curPos)) {
  130.                 FrameRect (range);    /*Undraw*/
  131.                 curPos = newPos;
  132.                 Pt2Rect (anchorPt, curPos, range);
  133.                 FrameRect (range);    /*Draw*/
  134.             }
  135.             lastTicks = TickCount ();
  136.         }
  137.     } /*while*/
  138.     
  139.     FrameRect (range);    /*Undraw*/
  140.     
  141.     SetPenState (&savePen);
  142.  
  143.     return (!EmptyRect (range));
  144. } /*TrackRange*/
  145.  
  146. /*----------*/
  147. static void PinBotRight  (Rect        limitRect,
  148.                           Rect        *sizeRect);
  149. static void PinBotRight  (Rect        limitRect,
  150.                           Rect        *sizeRect)
  151. {
  152.     #define lR    limitRect
  153.     if (sizeRect->bottom < lR.top)        sizeRect->bottom = lR.top;
  154.     if (sizeRect->right < lR.left)        sizeRect->right = lR.left;
  155.     if (sizeRect->bottom > lR.bottom)    sizeRect->bottom = lR.bottom;
  156.     if (sizeRect->right > lR.right)        sizeRect->right = lR.right;
  157.     #undef lR
  158. } /*PinBotRight*/
  159.  
  160. /*----------*/
  161. static Boolean InSlop     (Rect    *sizeRect,
  162.                          Rect    *slopRect);
  163. static Boolean InSlop     (Rect    *sizeRect,
  164.                          Rect    *slopRect)
  165. {
  166.     Boolean        isIn;
  167.     Point        bR;
  168.     
  169.     bR.v = sizeRect->bottom;
  170.     bR.h = sizeRect->right;
  171.     isIn = PtInRect (bR, slopRect);
  172.     return (isIn);
  173. } /*InSlop*/
  174.  
  175. /*----------*/
  176. Boolean TrackRect  (Rect            *sizeRect,
  177.                     Rect            limitRect,
  178.                     DragProcPtr        actionProc)
  179. {
  180.     PenState        savePen;
  181.     Point            curPos;
  182.     Point            newPos;
  183.     Point            offset;
  184.     Point            bR;
  185.     Rect            slopRect;
  186.     long            lastTicks;
  187.     long            temp;
  188.  
  189.     GetPenState (&savePen);
  190.     PenNormal ();
  191.     PenMode (notPatXor);
  192.     PenPat (&qd.gray);
  193.  
  194.     slopRect = limitRect;
  195.     InsetRect (&slopRect, -20, -20);
  196.     
  197.     GetMouse (&curPos);
  198.     bR.v = sizeRect->bottom;
  199.     bR.h = sizeRect->right;
  200.     temp = DeltaPoint (bR, curPos);
  201.     offset = *(Point *) &temp;
  202.  
  203.     FrameRect (sizeRect);    /*Draw*/
  204.  
  205.     lastTicks = 0;    
  206.     while (StillDown ()) {
  207.         if (TickCount () >= lastTicks + 2) {
  208.             GetMouse (&newPos);
  209.             if (!EqualPt (newPos, curPos)) {
  210.                 if (InSlop (sizeRect, &slopRect)) {
  211.                     FrameRect (sizeRect);    /*Undraw*/
  212.                 }
  213.                 curPos = newPos;
  214.                 AddPt (offset, &newPos);
  215.                 sizeRect->bottom = newPos.v;    /*botRight (sizeRect) = newPos;*/
  216.                 sizeRect->right = newPos.h;
  217.                 if (InSlop (sizeRect, &slopRect)) {
  218.                     PinBotRight (limitRect, sizeRect);
  219.                     FrameRect (sizeRect);    /*Draw*/
  220.                 }
  221.                 if (actionProc != NULL) {
  222.                     (*actionProc) ();
  223.                 }
  224.             }
  225.             lastTicks = TickCount ();
  226.         }
  227.     } /*while*/
  228.  
  229.     if (InSlop (sizeRect, &slopRect)) {
  230.         FrameRect (sizeRect);    /*Undraw*/
  231.     }
  232.     
  233.     SetPenState (&savePen);
  234.     
  235.     return (InSlop (sizeRect, &slopRect));
  236. } /*TrackRect*/
  237.  
  238. /*----------*/
  239. static void FrameLine (Rect        sizeRect);
  240. static void FrameLine (Rect        sizeRect)
  241. {
  242.     MoveTo (sizeRect.left, sizeRect.top);
  243.     LineTo (sizeRect.right - 1, sizeRect.bottom - 1);
  244. } /*FrameLine*/
  245.  
  246. /*----------*/
  247. Boolean TrackLine  (Rect            *sizeRect,
  248.                     Rect            limitRect,
  249.                     DragProcPtr        actionProc)
  250. {
  251.     PenState        savePen;
  252.     Point            curPos;
  253.     Point            newPos;
  254.     Point            Offset;
  255.     Point            bR;
  256.     Rect            slopRect;
  257.     long            lastTicks;
  258.     long            temp;
  259.  
  260.     GetPenState (&savePen);
  261.     PenNormal ();
  262.     PenMode (notPatXor);
  263.     PenPat (&qd.gray);
  264.  
  265.     slopRect = limitRect;
  266.     InsetRect (&slopRect, -20, -20);
  267.     
  268.     GetMouse (&curPos);
  269.     bR.v = sizeRect->bottom;
  270.     bR.h = sizeRect->right;
  271.     temp = DeltaPoint (bR, curPos);
  272.     Offset = *(Point *) &temp;
  273.  
  274.     FrameLine (*sizeRect);    /*Draw*/
  275.  
  276.     lastTicks = 0;    
  277.     while (StillDown ()) {
  278.         if (TickCount () >= lastTicks + 2) {
  279.             GetMouse (&newPos);
  280.             if (!EqualPt (newPos, curPos)) {
  281.                 if (InSlop (sizeRect, &slopRect)) {
  282.                     FrameLine (*sizeRect);    /*Undraw*/
  283.                 }
  284.                 curPos = newPos;
  285.                 AddPt (Offset, &newPos);
  286.                 sizeRect->bottom = newPos.v;    /*botRight (sizeRect) = newPos;*/
  287.                 sizeRect->right = newPos.h;
  288.                 /* ?? constrain line to be horizontal or vertical ?? */
  289.                 /*    With sizeRect do {
  290.                 /*        if ((right - left) > (bottom - top)) {
  291.                 /*            bottom = top + 1;
  292.                 /*        } else {
  293.                 /*            right = left + 1;
  294.                 /*        }
  295.                 /*    } /*with*/
  296.                 if (InSlop (sizeRect, &slopRect)) {
  297.                     PinBotRight (limitRect, sizeRect); 
  298.                     FrameLine (*sizeRect);    /*Draw*/
  299.                 }
  300.                 if (actionProc != NULL) {
  301.                     (*actionProc) ();
  302.                 }
  303.             }
  304.             lastTicks = TickCount ();
  305.         }
  306.     } /*while*/
  307.  
  308.     if (InSlop (sizeRect, &slopRect)) {
  309.         FrameLine (*sizeRect);    /*Undraw*/
  310.     }
  311.     
  312.     SetPenState (&savePen);
  313.     
  314.     return (InSlop (sizeRect, &slopRect));
  315. } /*TrackLine*/
  316.